const http = require('http')
const app = http.createServer()

app.on('request', (request, response) => {
    //判断请求方式
    const method = request.method.toLowerCase()
    //获取请求地址
    const {pathname} = new URL(request.url, 'http://localhost:3000')
    response.writeHead(200, {
        'content-type': 'text/html;charset=utf8'
    })
    if(method == 'get') {  
        if(pathname == '/' || pathname == '/index'){
            response.end('欢迎来到主页')
        }else if(pathname == '/list') {
            response.end('欢迎来到列表页')
        }else {
            response.end('您访问的页面不存在')
        }
    } else if (method == 'post') {
        if(pathname == '/' || pathname == '/index'){
            response.end('欢迎来到主页')
        }else if(pathname == '/list') {
            response.end('欢迎来到列表页')
        }else {
            response.end('您访问的页面不存在')
        }
    } 
})
app.listen(3000, () => {
    console.log("服务启动成功...")
})